package com.imageim.yimirror.ui.adapter;

import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.imageim.yimirror.R;


/**
 * Created by Yuaihen.
 * on 2018/11/27
 * 折叠的ListView
 */
public class ExpandListAdapter extends BaseExpandableListAdapter {
    private String[] grounpItem;
    private String[] childItem;
    private boolean[] isCheckList;
    //用于存放Indicator的集合
    private SparseArray<ImageView> mIndicators;
    private ExpandableListView.OnGroupExpandListener mOnGroupExpandedListener;

    public ExpandListAdapter(String[] grounpItem, String[] childItem) {
        this.grounpItem = grounpItem;
        this.childItem = childItem;
        mIndicators = new SparseArray<>();
    }

    public void updateAdapter(String[] grounpItem, String[] childItem, boolean[] isCheckList) {
        this.grounpItem = grounpItem;
        this.childItem = childItem;
        this.isCheckList = isCheckList;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return grounpItem == null ? 0 : grounpItem.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childItem == null ? 0 : childItem.length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return grounpItem[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childItem[childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.expand_group, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = convertView.findViewById(R.id.tv_group);
            groupViewHolder.ivIndicator = convertView.findViewById(R.id.iv_indicator);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        groupViewHolder.tvTitle.setText(grounpItem[groupPosition]);
        groupViewHolder.tvTitle.setTextColor(Color.parseColor("#ffffff"));

        //把位置和图标添加到Map
        mIndicators.put(groupPosition, groupViewHolder.ivIndicator);
        //根据分组状态设置Indicator
        setIndicatorState(groupPosition, isExpanded);
        return convertView;
    }

    public void setGroupTitle(String str) {
        if (grounpItem != null) {
            grounpItem[0] = str;
        }
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.expand_child_item, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvTitle = convertView.findViewById(R.id.tv_child_item);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }


        childViewHolder.tvTitle.setText(childItem[childPosition]);
        //如果状态是选中的话  需要设置为蓝色字体  否则就是白色
        if (isCheckList != null && isCheckList[childPosition]) {
            childViewHolder.tvTitle.setTextColor(Color.parseColor("#ff00a8"));
        } else {
            childViewHolder.tvTitle.setTextColor(Color.parseColor("#ffffff"));
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        if (mOnGroupExpandedListener != null) {
            mOnGroupExpandedListener.onGroupExpand(groupPosition);
        }
    }

    public void setOnGroupExpandedListener(ExpandableListView.OnGroupExpandListener onGroupExpandedListener) {
        mOnGroupExpandedListener = onGroupExpandedListener;
    }

    /**
     * 根据分组的展开闭合状态设置指示器
     */
    public void setIndicatorState(int groupPosition, boolean isExpanded) {
        if (isExpanded) {
            mIndicators.get(groupPosition).setImageResource(R.drawable.ic_arrow_down);
        } else {
            mIndicators.get(groupPosition).setImageResource(R.drawable.ic_arrow_left);
        }
    }

    static class GroupViewHolder {
        TextView tvTitle;
        ImageView ivIndicator;
    }

    static class ChildViewHolder {
        TextView tvTitle;
    }
}
